<?php

class UserController extends Zend_Controller_Action {

    protected $userId = null;
    protected $auth = null;

    public function init() {
        $this->auth = $this->view->auth_user;
        $this->userId = $this->auth->id_user;
        $this->view->headMeta()->appendHttpEquiv('expires', 'Wed, 26 Feb 1997 08:21:57 GMT')
                ->appendHttpEquiv('pragma', 'no-cache')
                ->appendHttpEquiv('Cache-Control', 'no-cache');
    }

    public function indexAction() {
        $user = new Application_Model_DbTable_User();
        $this->view->user = $user->getUser($this->userId);
        // var_dump($this->view->auth_user);
    }

    public function addAction() {

        // echo "<h1>Add</h1>";
        $form = new Application_Form_User('Ajouter', 'Nouveau', $this->auth);
        $this->view->form = $form;

//        $this->getResponse()->setHeader('Expires', '', true);
//        $this->getResponse()->setHeader('Cache-Control', 'public', true);
//        $this->getResponse()->setHeader('Cache-Control', 'no-cache');
//        $this->getResponse()->setHeader('Pragma', 'no-cache', true);


        if ($this->getRequest()->isPost()) {

            $data = $this->getRequest()->getPost();

            Zend_Debug::dump($data);

            if ($form->isValid($data)) {
                $user = new Application_Model_DbTable_User();
                $dataUser = $user->addUser($form->getValidValues($data));

                if ($this->sendRegisterMail($dataUser)) {
                    $this->_helper->redirector('index');
                } else {
                    throw new Zend_Exception("L'envoi du mail a échoué!");
                }
            } else {

                $form->populate($data);
            }
        }
    }

    public function editAction() {
        $form = new Application_Form_User('Enregistrer', 'Edition', $this->auth);
        $this->view->form = $form;






        if ($this->getRequest()->isPost()) {
            $data = $this->getRequest()->getPost();

            if ($form->isValid($data)) {
                //Zend_Debug::dump();
                // $dataUser = $form->getValidValues($data);

                $user = new Application_Model_DbTable_User();
                $dataUser = $user->editUser($form->getValidValues($data));

                //Zend_Debug::dump($form->getValidValues($data));
                //die;

                if ($this->sendRegisterMail($dataUser)) {
                    $this->_helper->redirector('index');
                } else {
                    throw new Zend_Exception("L'envoi du mail a échoué!");
                }
            } else {
                $form->populate($data);
            }
        } else {
            $id = $this->userId; //$this->_getParam('id', 0);
            if ($id > 0) {
                $user = new Application_Model_DbTable_User();
                $form->populate($user->getUser($id));
            }
        }
    }

    public function deleteAction() {
        // action body
    }

    public function passwordAction() {
        $form = new Application_Form_ChangePassword($this->auth);
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $data = $this->getRequest()->getPost();

            if ($form->isValid($data)) {
                $user = new Application_Model_DbTable_User();
                $user->changePassword($form->getValidValues($data));
                $this->_helper->redirector('index');
            } else {
                $form->populate($data);
            }
        } else {
            $id = $this->userId; //$this->_getParam('id', 0);
            if ($id > 0) {
                $user = new Application_Model_DbTable_User();
                $form->populate(array('id_user' => $id));
            }
        }
    }

    // TODO : deplacer sendRegisterMail() dans le controller user
    private function sendRegisterMail($user) {

        Zend_Debug::dump($user);

        $view = new Zend_View();
        $view->addScriptPath(APPLICATION_PATH . '/views/scripts/templates/');
        $view->user = $user;

        $html_body = $view->render('email.phtml');

        // Zend_Debug::dump($html_body);
        // die;

        $mail = new Zend_Mail('utf-8');
        $mail->setType(Zend_Mime::MULTIPART_RELATED);
        $mail->setBodyHtml($html_body);

        //$mail->setFrom('support@example.com', 'Envoi mail');
        $mail->addTo('macgile@gmail.com', 'Gilles');
        $mail->setSubject('Enregistrement Cabinet');

        //Zend_Debug::dump($mail);

        return $mail->send();

        //var_dump($result);
    }

}

